#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProgressBar>
#include <QLabel>
#include <QPlainTextEdit>
#include <QTime>
#include <QTimer>
#include <QTreeWidgetItem>
#include "phonehandler.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    enum
    {
        // how often to poll for the phone when it in enabled
        PollInterval = 1000,

        // columns in the memory region table
        COL_Desc = 0,
        COL_Addr,
        COL_Size,
        COL_FileName,
        COL_Type
    };
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QPlainTextEdit *LogWindow();

protected:
    PhoneHandler *ph;
    MemDumpThread *mdThread;
    QTimer *pollingTimer;
    QProgressBar *progressBar;
    QLabel *statusRight;
    QTime dumpStart;
    QString browseDir;
    u32 chunkViewMode;

    // change the gui around for different phone modes
    void SetupGui( PhoneHandler::DeviceMode m );
    void SetGuiForThreadDone();
    
private slots:
    void PollingTimerTimeOut();

    void on_actionDetect_Phone_toggled( bool arg1 );

    void on_actionDump_Region_triggered();

    void on_treeWidget_memRegions_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);

    void ThreadDone( quint32 address, quint32 size, const QString &path );
    void ThreadFatalError( const QString &e );

    void on_pushButton_dumpRegion_clicked();

    void on_pushButton_reset_clicked();

    void on_pushButton_BootDlMode_clicked();

    void on_pushButton_dumpChunk_clicked();

    void on_actionClear_Log_triggered();

    void on_pushButton_nvRead_clicked();

public slots:
    void OutputMessage( QtMsgType type, const QString &msg );

private:
    Ui::MainWindow *ui;

};

class LogBrowser : public QObject
{
    Q_OBJECT
public:
    explicit LogBrowser( MainWindow *win ) : QObject( win ), w( win )
    {
        qRegisterMetaType<QtMsgType>("QtMsgType");
        connect(this, SIGNAL( sendMessage(QtMsgType,QString)), w, SLOT(OutputMessage(QtMsgType,QString)), Qt::QueuedConnection );
    }

public slots:
    void outputMessage( QtMsgType type, const QString &msg )
    {
        emit sendMessage( type, msg );
    }
signals:
    void sendMessage( QtMsgType type, const QString &msg );

private:
    MainWindow *w;

};

#endif // MAINWINDOW_H
